﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Assets.Scripts.DynamicClouds
{
	// Token: 0x0200028A RID: 650
	public class CloudHub : MonoBehaviour
	{
		// Token: 0x06000E54 RID: 3668 RVA: 0x0006EDC0 File Offset: 0x0006CFC0
		private void Start()
		{
			this.ColliderSize = GameController.cloudController.CloudScale - GameController.cloudController.CloudScale * 0.05f;
			this.MyCollider.size = new Vector3((float)GameController.cloudController.CloudSpawnAreaRandomX * this.ColliderSize * 2f, (float)GameController.cloudController.CloudSpawnAreaRandomHieght * this.ColliderSize * 2f, (float)GameController.cloudController.CloudSpawnAreaRandomX * this.ColliderSize * 2f);
			this.MyCollider.center = new Vector3(0f, (float)GameController.cloudController.CloudSpawnAreaRandomHieght * this.ColliderSize, 0f);
		}

		// Token: 0x06000E55 RID: 3669 RVA: 0x0006EE74 File Offset: 0x0006D074
		private void Update()
		{
			if (this.Ready && GameController.playerInstance != null)
			{
				if (GameController.cloudController.CloudLODEnabled || GameController.cloudController.ActiveShadows)
				{
					this.mPlayerScale = GameController.playerInstance.gameObject.transform.lossyScale.y;
					this.mDistanceFromPlayer = Vector3.Distance(GameController.playerInstance.gameObject.transform.position, base.transform.position);
					if (this.mDistanceFromPlayer > GameController.cloudController.MidDistanceLOD * 4f * GameController.playerInstance.gameObject.transform.lossyScale.y)
					{
						this.mCurrentDistanceFromCloudRating = 3;
					}
					else if (this.mDistanceFromPlayer > GameController.cloudController.MidDistanceLOD * 2f * GameController.playerInstance.gameObject.transform.lossyScale.y)
					{
						this.mCurrentDistanceFromCloudRating = 2;
					}
					else if (this.mDistanceFromPlayer > GameController.cloudController.MidDistanceLOD * GameController.playerInstance.gameObject.transform.lossyScale.y)
					{
						this.mCurrentDistanceFromCloudRating = 1;
					}
					else
					{
						this.mCurrentDistanceFromCloudRating = 0;
					}
					if (this.mCurrentDistanceFromCloudRating != this.mPreviousDistanceFromCloudRating && !this.BusyLODSeting)
					{
						base.StartCoroutine(this.LimitedCloudLOD(this.mCurrentDistanceFromCloudRating));
					}
				}
				if (GameController.cloudController.ActiveShadows)
				{
					if (GameController.cloudController.PlayerProximityCanActivateShadowChecks)
					{
						this.PlayerIsNearEnoughForShadows = false;
						if (this.mDistanceFromPlayer < GameController.cloudController.CloudPlayerActivationRange)
						{
							this.PlayerIsNearEnoughForShadows = true;
						}
						else
						{
							this.PlayerIsNearEnoughForShadows = false;
						}
					}
					else
					{
						this.PlayerIsNearEnoughForShadows = false;
					}
					this.NumberOfGtsNear = 0;
					if (GameController.cloudController.ActiveShadows)
					{
						foreach (GameObject gameObject in GameController.giantessesOnScene)
						{
							if (Vector3.Distance(gameObject.transform.position, base.transform.position) < GameController.cloudController.CloudGtsActivationRange * this.mPlayerScale)
							{
								this.NumberOfGtsNear++;
							}
						}
						if (this.NumberOfGtsNear > 0 || this.PlayerIsNearEnoughForShadows)
						{
							if (GameController.cloudController.ActiveShadows)
							{
								this.mShadowsInactive = false;
							}
						}
						else
						{
							this.mShadowsInactive = true;
						}
					}
				}
				else
				{
					this.mShadowsInactive = true;
				}
				if (this.mItemsInBounds != 0)
				{
					this.mCurrentPhysicsState = true;
				}
				else if (GameController.cloudController.BeingInShadowRangeOverridesPhysics && !this.mShadowsInactive)
				{
					this.mCurrentPhysicsState = true;
				}
				else if (this.mItemsInBounds == 0)
				{
					this.mCurrentPhysicsState = false;
				}
				if (!this.mShadowsInactive && !this.BusyShadowChecking)
				{
					base.StartCoroutine(this.LimitedShadowChecks());
				}
				if (this.mCurrentPhysicsState != this.mPreviousPhysicsState)
				{
					if (!GameController.cloudController.CloudSleepingByCoroutine)
					{
						if (this.mCurrentPhysicsState)
						{
							this.ActivateCloudPhysics();
						}
						else if (!this.BusyShadowChecking)
						{
							this.DeactivateCloudPhysics();
						}
					}
					else if (!this.BusyChangingPhysics)
					{
						if (this.mCurrentPhysicsState)
						{
							base.StartCoroutine(this.LimitedPhysicsChange(true));
						}
						else if (!this.BusyShadowChecking)
						{
							base.StartCoroutine(this.LimitedPhysicsChange(false));
						}
					}
				}
			}
		}

		// Token: 0x06000E56 RID: 3670 RVA: 0x0006F240 File Offset: 0x0006D440
		private void OnTriggerEnter(Collider other)
		{
			this.mItemsInBounds++;
		}

		// Token: 0x06000E57 RID: 3671 RVA: 0x0006F250 File Offset: 0x0006D450
		private void OnTriggerExit(Collider other)
		{
			this.mItemsInBounds--;
		}

		// Token: 0x06000E58 RID: 3672 RVA: 0x0006F260 File Offset: 0x0006D460
		private void PlaceCloudTile(Vector3 position)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.TileCloud, position, base.transform.localRotation, base.transform);
			gameObject.transform.localPosition = position;
			Clouds component = gameObject.GetComponent<Clouds>();
			this.CloudScripts.Add(component);
			component.InitializeCloudTile(GameController.cloudController.CloudScale, position);
			if (GameController.cloudController.DisableCloudElastisity)
			{
				component.DisableCloudElasticity = true;
			}
		}

		// Token: 0x06000E59 RID: 3673 RVA: 0x0006F2D4 File Offset: 0x0006D4D4
		private void ActivateCloudPhysics()
		{
			int num = 0;
			foreach (Clouds clouds in this.CloudScripts)
			{
				this.CloudScripts[num].Rbody.isKinematic = false;
				this.CloudScripts[num].MainCollider.enabled = true;
				this.CloudScripts[num].GtsColider.enabled = true;
				this.CloudScripts[num].GtsColider.enabled = true;
				num++;
			}
			this.mPreviousPhysicsState = true;
		}

		// Token: 0x06000E5A RID: 3674 RVA: 0x0006F394 File Offset: 0x0006D594
		private void DeactivateCloudPhysics()
		{
			int num = 0;
			foreach (Clouds clouds in this.CloudScripts)
			{
				this.CloudScripts[num].Rbody.isKinematic = true;
				this.CloudScripts[num].MainCollider.enabled = false;
				this.CloudScripts[num].GtsColider.enabled = false;
				this.CloudScripts[num].GtsColider.enabled = false;
				num++;
			}
			this.mPreviousPhysicsState = false;
		}

		// Token: 0x06000E5B RID: 3675 RVA: 0x0006F454 File Offset: 0x0006D654
		public IEnumerator LimitedPhysicsChange(bool onOff)
		{
			this.BusyChangingPhysics = true;
			int i2 = 0;
			foreach (Clouds p in this.CloudScripts)
			{
				this.CloudScripts[i2].Rbody.isKinematic = !onOff;
				this.CloudScripts[i2].MainCollider.enabled = onOff;
				this.CloudScripts[i2].GtsColider.enabled = onOff;
				i2++;
				GameController.cloudController.ChangedPhysicsStates++;
				if (GameController.cloudController.ChangedPhysicsStates > GameController.cloudController.CloudPhysicsWakeSleepPerFrame - 1)
				{
					GameController.cloudController.ChangedPhysicsStates = 0;
					yield return null;
				}
			}
			this.mPreviousPhysicsState = onOff;
			this.BusyChangingPhysics = false;
			yield break;
		}

		// Token: 0x06000E5C RID: 3676 RVA: 0x0006F478 File Offset: 0x0006D678
		public IEnumerator LimitedShadowChecks()
		{
			this.BusyShadowChecking = true;
			foreach (Clouds c in this.CloudScripts)
			{
				c.ShadowDetectorScript.ShadowCheck();
				GameController.cloudController.CheckedShadows++;
				if (GameController.cloudController.CheckedShadows > GameController.cloudController.CloudShadowsCheckedPerFrame - 1)
				{
					GameController.cloudController.CheckedShadows = 0;
					yield return null;
				}
			}
			this.BusyShadowChecking = false;
			yield break;
		}

		// Token: 0x06000E5D RID: 3677 RVA: 0x0006F494 File Offset: 0x0006D694
		public IEnumerator LimitedCloudLOD(int level)
		{
			this.BusyLODSeting = true;
			foreach (Clouds cl in this.CloudScripts)
			{
				cl.setLOD(level);
				GameController.cloudController.SetLODs++;
				if (GameController.cloudController.SetLODs > GameController.cloudController.CloudLODAdjustmentsPerFrame - 1)
				{
					GameController.cloudController.SetLODs = 0;
					yield return null;
				}
			}
			this.mPreviousDistanceFromCloudRating = level;
			this.BusyLODSeting = false;
			yield break;
		}

		// Token: 0x06000E5E RID: 3678 RVA: 0x0006F4B8 File Offset: 0x0006D6B8
		public void GenerateCumulous()
		{
			for (int i = 0; i < GameController.cloudController.CloudSpawnAreaRandomHieght; i++)
			{
				this.mCurrentLayer = i;
				if (i == 0)
				{
					this.mCloudShape = new int[GameController.cloudController.CloudSpawnAreaRandomX, GameController.cloudController.CloudSpawnAreaRandomZ];
					this.RandomCloudBace();
					for (int j = 0; j < 5; j++)
					{
						this.SmoothClouds();
					}
				}
				else
				{
					this.NextCloudLayerAdjustments();
				}
				this.PlaceTiles();
			}
			foreach (Clouds clouds in this.CloudScripts)
			{
				clouds.ShadowDetector.SetActive(true);
				clouds.ShadowDetectorScript.ShadowCheck();
			}
			this.DeactivateCloudPhysics();
			this.Ready = true;
		}

		// Token: 0x06000E5F RID: 3679 RVA: 0x0006F5A8 File Offset: 0x0006D7A8
		private void RandomCloudBace()
		{
			this.mSeed = DateTime.Now.ToString();
			System.Random random = new System.Random(this.mSeed.GetHashCode());
			for (int i = 0; i < GameController.cloudController.CloudSpawnAreaRandomX; i++)
			{
				for (int j = 0; j < GameController.cloudController.CloudSpawnAreaRandomZ; j++)
				{
					if (i == 0 || i == GameController.cloudController.CloudSpawnAreaRandomX - 1 || j == 0 || j == GameController.cloudController.CloudSpawnAreaRandomZ - 1)
					{
						this.mCloudShape[i, j] = 1;
					}
					else
					{
						this.mCloudShape[i, j] = ((random.Next(0, 100) >= GameController.cloudController.CloudSpawnAreaRandomFillPercent) ? 0 : 1);
					}
				}
			}
		}

		// Token: 0x06000E60 RID: 3680 RVA: 0x0006F684 File Offset: 0x0006D884
		private void SmoothClouds()
		{
			for (int i = 0; i < GameController.cloudController.CloudSpawnAreaRandomX; i++)
			{
				for (int j = 0; j < GameController.cloudController.CloudSpawnAreaRandomZ; j++)
				{
					int num = this.FindNeighboringCloudTiles(i, j);
					if (num > 4)
					{
						this.mCloudShape[i, j] = 1;
					}
					else if (num < 4)
					{
						this.mCloudShape[i, j] = 0;
					}
				}
			}
		}

		// Token: 0x06000E61 RID: 3681 RVA: 0x0006F700 File Offset: 0x0006D900
		private int FindNeighboringCloudTiles(int gridX, int gridZ)
		{
			int num = 0;
			for (int i = gridX - 1; i <= gridX + 1; i++)
			{
				for (int j = gridZ - 1; j <= gridZ + 1; j++)
				{
					if (i >= 0 && i < GameController.cloudController.CloudSpawnAreaRandomX && j >= 0 && j < GameController.cloudController.CloudSpawnAreaRandomZ)
					{
						if (i != gridX || j != gridZ)
						{
							num += this.mCloudShape[i, j];
						}
					}
					else
					{
						num++;
					}
				}
			}
			return num;
		}

		// Token: 0x06000E62 RID: 3682 RVA: 0x0006F794 File Offset: 0x0006D994
		private void PlaceTiles()
		{
			float cloudScale = GameController.cloudController.CloudScale;
			if (this.mCloudShape != null)
			{
				for (int i = 0; i < GameController.cloudController.CloudSpawnAreaRandomX; i++)
				{
					for (int j = 0; j < GameController.cloudController.CloudSpawnAreaRandomZ; j++)
					{
						if (this.mCloudShape[i, j] == 0 || GameController.cloudController.Overcast)
						{
							this.PlaceCloudTile(new Vector3((float)i * (cloudScale * 2f) - cloudScale * (float)GameController.cloudController.CloudSpawnAreaRandomX, (float)this.mCurrentLayer * (cloudScale * 2f), (float)j * (cloudScale * 2f) - cloudScale * (float)GameController.cloudController.CloudSpawnAreaRandomZ));
						}
					}
				}
			}
		}

		// Token: 0x06000E63 RID: 3683 RVA: 0x0006F858 File Offset: 0x0006DA58
		private void NextCloudLayerAdjustments()
		{
			for (int i = 0; i < GameController.cloudController.CloudSpawnAreaRandomX; i++)
			{
				for (int j = 0; j < GameController.cloudController.CloudSpawnAreaRandomZ; j++)
				{
					int num = this.FindNeighboringCloudTiles(i, j);
					if (9 - num < UnityEngine.Random.Range(0, GameController.cloudController.CloudSpawnAreaRandomReductionFactorPerLayer))
					{
						this.mCloudShape[i, j] = 1;
					}
				}
			}
		}

		// Token: 0x0400135C RID: 4956
		public GameObject TileCloud;

		// Token: 0x0400135D RID: 4957
		public BoxCollider MyCollider;

		// Token: 0x0400135E RID: 4958
		private int mTilesLaid;

		// Token: 0x0400135F RID: 4959
		private int mRowsLaid;

		// Token: 0x04001360 RID: 4960
		[HideInInspector]
		public float ColliderSize;

		// Token: 0x04001361 RID: 4961
		[HideInInspector]
		public List<Clouds> CloudScripts = new List<Clouds>();

		// Token: 0x04001362 RID: 4962
		private int mItemsInBounds;

		// Token: 0x04001363 RID: 4963
		private bool mCurrentPhysicsState;

		// Token: 0x04001364 RID: 4964
		private bool mPreviousPhysicsState;

		// Token: 0x04001365 RID: 4965
		private bool mShadowsInactive;

		// Token: 0x04001366 RID: 4966
		[HideInInspector]
		public bool Ready;

		// Token: 0x04001367 RID: 4967
		private int mCurrentLayer;

		// Token: 0x04001368 RID: 4968
		private string mSeed;

		// Token: 0x04001369 RID: 4969
		private int[,] mCloudShape;

		// Token: 0x0400136A RID: 4970
		[HideInInspector]
		public bool BusyChangingPhysics;

		// Token: 0x0400136B RID: 4971
		[HideInInspector]
		public bool BusyShadowChecking;

		// Token: 0x0400136C RID: 4972
		[HideInInspector]
		public bool BusyLODSeting;

		// Token: 0x0400136D RID: 4973
		[HideInInspector]
		public bool PlayerIsNearEnoughForShadows;

		// Token: 0x0400136E RID: 4974
		[HideInInspector]
		public int NumberOfGtsNear;

		// Token: 0x0400136F RID: 4975
		private bool mWaitForShadowCheckToDeactivatePhysics;

		// Token: 0x04001370 RID: 4976
		private float mDistanceFromPlayer;

		// Token: 0x04001371 RID: 4977
		private float mPlayerScale;

		// Token: 0x04001372 RID: 4978
		private int mCurrentDistanceFromCloudRating;

		// Token: 0x04001373 RID: 4979
		private int mPreviousDistanceFromCloudRating;
	}
}
